@inject NavigationManager nav;

<div class="layui-table-page">
    <div id="layui-table-page14">
        <div class="layui-box layui-laypage layui-laypage-default">
            <a href="@GetUrl(_preIndex)" class="layui-laypage-prev @_preClass">
                <i class="layui-icon"></i>
            </a>
            @for (; _preFirstIndex < PageIndex; _preFirstIndex++)
            {
                <a href="@GetUrl(_preFirstIndex)">@_preFirstIndex</a>
            }
            <span class="layui-laypage-curr">
                <em class="layui-laypage-em"></em><em>@PageIndex</em>
            </span>
            @for (var index = PageIndex + 1; index <= _preNextIndex; index++)
            {
                <a href="@GetUrl(index)">@index</a>
            }
            <a href="@GetUrl(_nextIndex)" class="layui-laypage-next @_nextClass">
                <i class="layui-icon"></i>
            </a><span class="layui-laypage-skip">到第<input type="text" min="1" @bind="_jumpIndex" @bind:event="oninput" class="layui-input">页<button type="button" class="layui-laypage-btn" @onclick="Jump">确定</button></span><span class="layui-laypage-count">共 @TotalCount 条</span>
            <span class="layui-laypage-limits">
                @PageSize 条/页
            </span>
        </div>
    </div>
</div>

@code {
    [Parameter] public int PageIndex { get; set; }
    [Parameter] public long TotalCount { get; set; }
    [Parameter] public int PageSize { get; set; }
    [Parameter] public string PageUrl { get; set; }

    private int _preIndex = 1;
    private int _nextIndex = 1;
    private string _preClass;
    private string _nextClass;

    /// 跳转页（默认PageIndex + 1)
    private int _jumpIndex;

    /// 分页列表，开始索引页
    private int _preFirstIndex = 0;

    /// 分页列表，结束索引页
    private int _preNextIndex = 0;

    protected override Task OnParametersSetAsync()
    {
        _preClass = "";
        _nextClass = "";
        _preFirstIndex = 0;
        _preNextIndex = 0;

        if (PageIndex < 1) PageIndex = 1;

        if (PageIndex == 1) _preIndex = 1;
        else _preIndex = PageIndex - 1;

        _nextIndex = PageIndex + 1;

        var maxIndex = (int) Math.Ceiling((double) TotalCount / (double) PageSize);
        if (PageIndex == 1) _preClass = "layui-disabled";
        if (PageIndex >= maxIndex)
        {
            _nextClass = "layui-disabled";
            _nextIndex = maxIndex;
        }

    // 分页列表
        _preFirstIndex = PageIndex - 5;
        if (_preFirstIndex < 1) _preFirstIndex = 1;

        _preNextIndex = PageIndex + 5;
        if (_preNextIndex > maxIndex) _preNextIndex = maxIndex;

    // 跳转页
        _jumpIndex = PageIndex + 1;
        if (_jumpIndex > maxIndex) _jumpIndex = PageIndex;
        return Task.FromResult(0);
    }

    /// 跳转
    private void Jump()
    {
        var url = GetUrl(_jumpIndex);
        nav.NavigateTo(url);
    }

    private string GetUrl(int pageIndex)
    {
        return PageUrl.Replace("{index}", pageIndex.ToString());
    }

}